package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;


//"id":"2021-2022-1-1304064030051111111111111111000000002103","zxjxjhh":"2021-2022-1-1","kch":"304064030","kxh":"05","kcm":"现代软件工程（双语）","xf":"3","xs":"48","kkxsh":"304","kkxsjc":"计算机学院","kslxdm":null,"kslxmc":null,"skjs":"李旭伟* ","bkskrl":72,"bkskyl":-23,"xkmsdm":"01","xkmssm":"直选式","xkkzdm":"01","xkkzsm":"可选可退","xkkzh":null,"xkxzsm":"允许院系专业 计算机学院\r\n;","kkxqh":"03","kkxqm":"江安","sfxzxslx":null,"sfxzxsnj":null,"sfxzxsxs":"是","sfxzxxkc":null,"sfxzxdlx":"否","xqh":"03","jxlh":"301","jash":"A607","jclxdm":"01","skzc":"111111111111111100000000","skxq":2,"skjc":10,"cxjc":3,"xqlxdm":"1","xqdm":"1","xss":95,"zcsm":"1-16周","kclbdm":null,"kclbmc":null,"xkbz":null,"xqm":"江安","jxlm":"一教A座","jasm":"A607"

public class ClassroomSerch extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classroom_serch);

        Button crSerchButton= (Button)findViewById(R.id.tv_search);



        //classroomTextview.setText("666");

        crSerchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    TextView classroomTextview;
                    classroomTextview = (TextView)findViewById(R.id.textView8);
                    classroomTextview.setMovementMethod(ScrollingMovementMethod.getInstance());

                    EditText jxlEditText;
                    jxlEditText = (EditText)findViewById(R.id.edit_search3);
                    EditText jsEditText;
                    jsEditText = findViewById(R.id.edit_search);

                    InputStreamReader isr = new InputStreamReader(getAssets().open("courses.json"),"UTF-8");
                    BufferedReader br = new BufferedReader(isr);
                    String line;
                    StringBuilder builder = new StringBuilder();

                    String gotJxl = jxlEditText.getText().toString();
                    String gotJs  = jsEditText.getText().toString();

                    while((line = br.readLine()) != null){
                        builder.append(line);
                    }
                    br.close();
                    isr.close();
                    JSONObject testjson = new JSONObject(builder.toString());//builder读取了JSON中的数据。
                    //直接传入JSONObject来构造一个实例
                    JSONArray array = testjson.getJSONArray("records");         //从JSONObject中取出数组对象
                    //dateTextView.setText(array.length());

                    //int num = 0;
                    String serchResult = "";
                    for (int i = 0; i < array.length();i++) {
                        JSONObject role = array.getJSONObject(i);    //取出数组中的对象
                        if (role.getString("jxlm").equals(gotJxl)&&role.getString("jasm").equals(gotJs)){

                            serchResult += role.getString("kcm")+"\t"+role.getString("skjs")+"星期"+role.getString("skxq")+"\t"+role.getString("zcsm")+" 第"+role.getString("skjc")+"节课 "+role.getString("cxjc")+"节次";
                            serchResult += "\n";
                        }
                    }
                    classroomTextview.setText(serchResult);


                    //System.out.println(num);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                }


        });
    }
}
